package com.zhht.checkcop.demo;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import com.zhht.checkcop.db.MysqlDb;
import com.zhht.checkcop.db.StaticDBMap;
import com.zhht.checkcop.parser.BaiduParserMovie;

/**
 * 电影初始化的时候用   以后不使用了
 * @author love
 *
 */
@Deprecated
public class BaiduMovieInitialization implements PageProcessor{
	private final static Logger log= Logger.getLogger(MovieInitialization.class);
	private Boolean pageURL=new Boolean(true);
	
	private Site site = Site.me().setCycleRetryTimes(5).setSleepTime(1500).setTimeOut(6000);
	private String classifyId;
	private String typeName;
	private String type;
	public BaiduMovieInitialization(){}
	public BaiduMovieInitialization(String typeName,String classifyId,String type){
		this.typeName = typeName;
		this.classifyId=classifyId;
		this.type = type;
	}

	public void process(Page page) {
    	try{
//    		System.out.println(page);
//    		System.out.println(page.getHtml());
	    	List<String> frontCoverUrl=page.getHtml().xpath("//div[@class='result-op c-container']/h3/a/@href").all();	
    		List<String> content=page.getHtml().xpath("//div[@class='lemma-summary']/div[@class='para']/text()").all();	
    		List<String> infoPicUrl = page.getHtml().xpath("//div[@class='summary-pic']/a/img/@src").all();	
//	    	List<String> infoUrl=page.getHtml().xpath("//div[@class='txt']/h3/a/@href").all();		//详情URL
//	    	
//	    	List<String> movieName = page.getHtml().xpath("//div[@class='txt']/h3/a/allText()").all();
//	    	
////	    	List<String> movieName=page.getHtml().xpath("//div[@class='txt']/h3/a/b/font/text()").all();	//电影名称
//	    	List<String> updateTime=page.getHtml().xpath("//div[@class='txt']/span/text()").all();//是否推荐
    		page.addTargetRequests(frontCoverUrl);
	    	if(null != content && content.size()>0){
//		    	page.putField("frontCoverUrl", frontCoverUrl);
		    	page.putField("infoUrl", content);
		    	page.putField("infoPicUrl", infoPicUrl);
//		    	page.putField("updateTime", updateTime);
//		    	page.putField("classifyId", classifyId);
//		    	page.putField("infoClassifyName", typeName);
		    	
		    	
		    	
//		    	String currentPage = page.getHtml().xpath("//li[@class='total']/span/text()").get();
//		    	log.info("当前类型:"+typeName+"------------------------------currentPage:"+currentPage);
//		    	if(pageURL){
//		    		System.out.println("pageURL:"+pageURL);
//		    		String pageNum = page.getHtml().xpath("//li[@class='total']/span/text()").get();
//		    		if(pageNum!=null&&pageNum.length()>2){
//		    			
//		    			pageNum=pageNum.substring(1, pageNum.indexOf("页"));
//		    			
//		    			page.addTargetRequests(getPageNum(Integer.parseInt(pageNum),type));
//		    		}
//		    		pageURL=false;
//		    	}
	    	}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
//    public List<String> getPageNum(Integer pageNum,String type){
//    	List<String> page=new ArrayList<String>();
//    	for(int i=2;i<=pageNum;i++){
//    		page.add("https://www.piaohua.com/html/"+type+"/list_"+i+".html");
//    	}
//    	return page;
//    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
    	List<Map<String, Object>> list=null;
    	try{
    		StaticDBMap.mysqldb = new MysqlDb();
    		StaticDBMap.mysqldb.connect();
        	list = StaticDBMap.mysqldb.query("select `name` from t_gf_authors where `name` NOT in('徐灿','王褒','陈沆','无名氏') AND baidu_info is NULL GROUP BY `name` ");
    	}catch (Exception e) {
			e.printStackTrace();
		}
    	int i=0;
    	for(Map<String,Object> mm:list){
    		System.out.println("更新："+mm.get("name"));
    		i++;
    		Spider.create(new BaiduMovieInitialization(mm.get("name")+"","1","dongzuo")).addUrl("https://baike.baidu.com/item/"+URLEncoder.encode(mm.get("name")+"", "UTF-8")).addPipeline(new BaiduParserMovie(mm.get("name")+"","https://www.baidu.com")).thread(1).run();
    		
    		if(i%10==0){
    			try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    		}
    	}
//    	System.out.println("https://www.baidu.com/s?ie=utf-8&wd="+URLEncoder.encode("李商隐", "UTF-8"));
//    	Spider.create(new BaiduMovieInitialization("动作片","1","dongzuo")).addUrl("http://www.baidu.com/s?wd="+URLEncoder.encode("李商隐", "UTF-8")).addPipeline(new BaiduParserMovie("dongzuo","https://www.baidu.com")).thread(1).run();
//    	int thread=args!=null&&args.length>0?Integer.parseInt(args[0]):10;
//	    StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("动作片","1","dongzuo")).addUrl("https://www.piaohua.com/html/dongzuo/index.html").addPipeline(new ParserMovie("dongzuo","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("动作片已完成");
        
        
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("喜剧片","2","xiju")).addUrl("https://www.piaohua.com/html/xiju/index.html").addPipeline(new ParserMovie("xiju","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("喜剧片已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("爱情片","3","aiqing")).addUrl("https://www.piaohua.com/html/aiqing/index.html").addPipeline(new ParserMovie("aiqing","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("爱情片已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("科幻片","4","kehuan")).addUrl("https://www.piaohua.com/html/kehuan/index.html").addPipeline(new ParserMovie("kehuan","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("科幻片已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("剧情片","5","juqing")).addUrl("https://www.piaohua.com/html/juqing/index.html").addPipeline(new ParserMovie("juqing","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("剧情片已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("悬疑片","6","xuannian")).addUrl("https://www.piaohua.com/html/xuannian/index.html").addPipeline(new ParserMovie("xuannian","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("悬疑片已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("战争片","7","zhanzheng")).addUrl("https://www.piaohua.com/html/zhanzheng/index.html").addPipeline(new ParserMovie("zhanzheng","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("战争片已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("恐怖片","8","kongbu")).addUrl("https://www.piaohua.com/html/kongbu/index.html").addPipeline(new ParserMovie("kongbu","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("恐怖片已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("灾难片","9","zainan")).addUrl("https://www.piaohua.com/html/zainan/index.html").addPipeline(new ParserMovie("zainan","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("灾难片已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("连续剧","10","lianxuju")).addUrl("https://www.piaohua.com/html/lianxuju/index.html").addPipeline(new ParserMovie("lianxuju","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("连续剧已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieInitialization("动漫","11","dongman")).addUrl("https://www.piaohua.com/html/dongman/index.html").addPipeline(new ParserMovie("dongman","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("动漫已完成");
    }
    
    
	private static void saveAndUpdate() {
		try{
        	MysqlDb mysqldb = new MysqlDb();
        	mysqldb.connect();
        	if(null != StaticDBMap.writeNovelNameMap && StaticDBMap.writeNovelNameMap.size() > 0){
	        	//新增
	        	Iterator<Entry<String, Map<String,Object>>> iter = StaticDBMap.writeNovelNameMap.entrySet().iterator();
	        	List<String[]> sqls=new ArrayList<String[]>();
	        	while (iter.hasNext()) {
	        		Map.Entry<String, Map<String,Object>> entry = iter.next();
	        		sqls.add(mysqldb.assemblyInsertSqls("movie_info",entry.getValue()));
	            }
	        	mysqldb.executeBatchs(sqls);
        	}
        	mysqldb.close();
        }catch (Exception e) {
        	e.printStackTrace();
        }
	}
}
